#ifndef _BASE_SINGLETON_H_
#define _BASE_SINGLETON_H_

#include <boost/make_shared.hpp>
#include <boost/thread.hpp>

namespace base {

template<class T> class Singleton {
public:
  static T& instance() {
    if (!__instance) {
      boost::mutex::scoped_lock lock(__mutex);
      if (!__instance) {
        __instance = boost::shared_ptr<T>(new T());
      }
    }
    return *__instance;
  }

protected:
  Singleton() {
  }
  virtual ~Singleton() {
  }

  Singleton(const Singleton& rhs);
  Singleton& operator=(const Singleton& rhs);

protected:
  static boost::shared_ptr<T> __instance;
  static boost::mutex __mutex;
};

template<class T> boost::shared_ptr<T> Singleton<T>::__instance;
template<class T> boost::mutex Singleton<T>::__mutex;

}

#endif
